﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MyRoguelike.Tools;
using Microsoft.Xna.Framework.Input;
using MyRoguelike.Game.Items;

namespace MyRoguelike.Game.Objects
{
    class GOPlayer : GameObject
    {
        private Inventory _inventory;

        // Player health
        private int _health;

        // Player inventory
        public Inventory Inventory
        {
            get { return _inventory; }
        }

        // Damage
        public int Damage { get; set; }

        // Defense
        public int Defense { get; set; }

        // Player health
        public int Health
        {
            get { return _health; }
            set
            {
                _health = value;
                if (_health > MaxHealth) _health = MaxHealth;
            }
        }

        // Player maximum health
        public int MaxHealth { get; set; }

        // Player is die?
        public bool IsDeath
        {
            get
            {
                if (Health <= 0) return true;
                return false;
            }
        }

        // Player sight range
        public float SightRange { get; set; }

        // Attenuation of sight const
        public const float AttenuationOfSight = 50f;


        public GOPlayer(Level level)
            : base(level)
        {
            IsPlayer = true;

            Texture = ResourceManager.Textures["Player"];

            _inventory = new Inventory(this);

            MaxHealth = 25;
            Health = 25;
            SightRange = 150;

            // Adding default weapon
            _inventory.AddItem(new GIRustySword()).On();
        }

        public override void Update(GameTime gameTime)
        {
            // Get player map coordinate
            Point playerMapPos = _level.GetObjectCoord(this);
            Point newPlayerMapPos = playerMapPos;

            // Move
            if (InputManager.KeyboardClick(Keys.W))
                newPlayerMapPos.Y -= 1;
            if (InputManager.KeyboardClick(Keys.A))
                newPlayerMapPos.X -= 1;
            if (InputManager.KeyboardClick(Keys.S))
                newPlayerMapPos.Y += 1;
            if (InputManager.KeyboardClick(Keys.D))
                newPlayerMapPos.X += 1;
            if (InputManager.KeyboardClick(Keys.Space))
            {
                _level.Step();
                return;
            }

            if (playerMapPos != newPlayerMapPos)
            {
                if (_level.MoveTo(this, newPlayerMapPos))
                    _level.Step();
            }
        }

        public override void Step()
        {

        }

        public override bool OnCollision(GameObject other)
        {
            //Todo

            return true;
        }

    }
}
